//
//  YZYActivityView.h
//  YZYSDKManagerDemo
//
//  Created by 赵隆杰 on 2024/6/7.
//

#import "YZYActivityView.h"
#import <WebKit/WebKit.h>
#import "YZYSDKManagerHeader.h"
#import "YZYSaccountStore.h"
#import "ZCFunDataFormat.h"

@interface YZYActivityView ()

@property (nonatomic, strong) UIButton *coverView;
@property (strong, nonatomic) WKWebView *wkWebView;

@property (nonatomic, strong) UILabel *titleLabel;


@end

@implementation YZYActivityView

- (instancetype)init {
    if (self == [super init]) {
                
        UIImageView *backbg = [[UIImageView alloc] init];
        backbg.userInteractionEnabled = YES;
        backbg.backgroundColor = [UIColor whiteColor];
        backbg.layer.masksToBounds = YES;
        backbg.layer.cornerRadius = 4;
        [self addSubview:backbg];
        backbg.frame = CGRectMake(0, 0, popupW, popupH_long);
        
        UIView *nameView = [[UIView alloc] init];
        nameView.backgroundColor = yzy_themeColor;
        [backbg addSubview:nameView];
        nameView.frame = CGRectMake(0, 0, backbg.frame.size.width, 40);
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont boldSystemFontOfSize:20];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.frame = CGRectMake(0, 0, nameView.frame.size.width, nameView.frame.size.height);
        titleLabel.textColor = [UIColor whiteColor];
        [nameView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UIButton *closeBtn = [[UIButton alloc] init];
        [nameView addSubview:closeBtn];
        closeBtn.frame = CGRectMake(nameView.frame.size.width - 40, 0, 40, 40);
        [closeBtn setImage:[[YZYSaccountStore sharedInstance] getImage:@"close_white"] forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(didClickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
        
        WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nameView.frame), popupW, popupH_long - CGRectGetMaxY(nameView.frame))];
        [backbg addSubview:webView];
        self.wkWebView = webView;
    }
    return self;
}

- (void)setUrlTitle:(NSString *)urlTitle {
    self.titleLabel.text = urlTitle;
}

- (void)setH5Url:(NSString *)h5Url {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:h5Url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
    [self.wkWebView loadRequest:request];
}

- (void)didClickCloseBtn {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    });
}

- (void)closeView {
    [self didClickCloseBtn];
}

- (void)show {
    [[YZYSaccountStore sharedInstance] getCurrentWindowWithCompletion:^(UIWindow *window) {
        if (window) {
            UIButton *coverView = [[UIButton alloc] init];
            coverView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
            self.coverView = coverView;
            coverView.frame = window.bounds;
            UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
            UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            visualView.alpha = 0.0;
            [coverView addSubview:visualView];
            visualView.frame = coverView.frame;
            
            [window addSubview:coverView];
            
            self.frame = CGRectMake(0, 0, popupW, popupH_long);
            self.layer.cornerRadius = 12.0f;
            self.center = CGPointMake(window.center.x, window.center.y);
            if (![ZCFunDataFormat GetIsLandscape]) {
                self.center = CGPointMake(window.center.x, window.center.y - 22);
            }
            [window addSubview:self];
        }
    }];
}

@end
